package com.bases {
	import com.skin.SkinManage;
	import com.tips.TipPanel;
	import com.tips.TipsManager;
	import com.utils.SCUiTool;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import org.bytearray.display.ScaleBitmap;

	/**
	 * 显示图标按钮
	 * @author pethan
	 *
	 */
	public class ImageButton extends TUIComponent {
		private var imageUp:Class;
		private var imageDown:Class;
		private var imageOver:Class;
		protected var _button:SimpleButton;
		private var _label:String;
		private var tips:String;
		private var scaleRect:Rectangle;
		//是否可以用
		private var _enabled:Boolean = true;
		public var tScale:Number = 1;

		/**
		 *
		 * @param _imageUp
		 * @param _imageDown
		 * @param _imageOver
		 * @param text
		 * @param tips
		 *
		 */
		public function ImageButton(_imageUp:Class, _imageDown:Class, _imageOver:Class, text:String = '', tips:String = ''){
			super();
			this.imageUp = _imageUp;
			this.imageDown = _imageDown;
			this.imageOver = _imageOver;
			this._label = text;
			_button = new SimpleButton();
			this.addChild(_button);
			this.addSkin();
			this.tips = tips;
			if (tips != ''){
				TipsManager.getInstance().setTipRenderer(this, tipRender);
			}
		}

		private function tipRender(tip:TipPanel):void {
			tip.clear();
			tip.addChild(SCUiTool.buildStaticTextField(tips));
		}

		public function set tipText(value:String):void {
			this.tips = value;
			TipsManager.getInstance().updateTip(this);
		}

		public function get tipText():String {
			return this.tips;
		}

		override public function setSize(w:Number, h:Number):void {
			if (scaleRect != null){
				this.addSkin(scaleRect, w, h);
			} else {
				_button.width = w;
				_button.height = h;
			}
		}

		/**
		 * 设置九切片的大小
		 * @param _x
		 * @param _y
		 * @param _width
		 * @param _height
		 *
		 */
		public function setScale9Grid(_x:Number, _y:Number, _width:Number, _height:Number):void {
			if (scaleRect == null){
				scaleRect = new Rectangle();
			}
			scaleRect.x = _x;
			scaleRect.y = _y;
			scaleRect.width = _width;
			scaleRect.height = _height;
		}

		public function set enabled(value:Boolean):void {
			_enabled = value;
			if (value){
				this.filters = null;
			} else {
				this.filters = [SCUiTool.buildGrayFilter()];
			}
			//super.enabled=value;
			super.mouseChildren = value;
			super.mouseEnabled = value;
		}

		/**
		 * 是否可以用
		 * @return bool
		 *
		 */
		public function get enabled():Boolean {
			return _enabled;
		}

		public function setNewImageButton(_imageUp:Class, _imageDown:Class, _imageOver:Class):void {
			this.imageUp = _imageUp;
			this.imageDown = _imageDown;
			this.imageOver = _imageOver;
			this.addSkin();
		}

		private function buildState(label:String, stateStr:String = 'up', rect:Rectangle = null, _width:Number = 0, _height:Number = 0):Sprite {
			var state:Sprite = new Sprite();
			var bmp:ScaleBitmap;
			switch (stateStr){
				case 'up':
					bmp = new ScaleBitmap(new imageUp(0, 0));
					break;
				case 'over':
					bmp = new ScaleBitmap(new imageOver(0, 0));
					break;
				case 'down':
					bmp = new ScaleBitmap(new imageDown(0, 0));
					break;
				default:
					bmp = new ScaleBitmap(new imageUp(0, 0));
					break;
			}
			if (rect != null){
				bmp.scale9Grid = rect;
				bmp.setSize(_width, _height);
			}
			state.addChild(bmp);
			state.mouseChildren = state.mouseEnabled = false;
			return state;
		}

		private function addSkin(rect:Rectangle = null, _width:Number = 0, _height:Number = 0):void {
			this._button.upState = buildState(_label, 'up', rect, _width, _height);
			this._button.downState = buildState(_label, 'down', rect, _width, _height);
			this._button.overState = buildState(_label, 'over', rect, _width, _height);
			this._button.hitTestState = this._button.upState;
		}

		public function setImageButtonSize(w:Number, h:Number):void {
			this.setSize(w, h);
		}

		/**
		 * 设置按钮的焦点是否可以用
		 * @param value
		 *
		 */
		override public function set tabEnabled(value:Boolean):void {
			this.tabChildren = value;
			super.tabEnabled = value;
		}

		/**
		 * Dispose Imagebutton对象
		 *
		 */
		override public function dispose():void {
			super.dispose();
			this.imageDown = null;
			this.imageOver = null;
			this.imageUp = null;
			this._button = null;
			this.scaleRect = null;
		}
	}
}